main.controller('pagesCtrl', ["$scope", "$rootScope", "$location", "tools", "pagesServ", "userConfig", "$timeout", function ($scope, $rootScope, $location, tools, pagesServ, userConfig, $timeout) {
    $scope.pages = [];
    $scope.folders = [];
    $scope.authorList = [];

    var page_index = -1;
    var folder_index = -1;
    var events = {};

    $scope.userName = window.localStorage.getItem('userName');

    // 用户级别的信息保存
    $scope.pageClassifies = pagesServ.getPageClassifies();
    $rootScope.$on('NEW_CLASSIFY_ADDED_EV', function () {
        $scope.pageClassifies = pagesServ.getPageClassifies();
    });

    // 搜标题或者作者
    $scope.query = {
        text: ''
    };
    $scope.$watch('query.text', function (nv, ov) {
        angular.forEach($scope.pages, function (page) {
            if (page.sharerName.indexOf(nv) != -1 || page.pageName.indexOf(nv) != -1) {
                page.shown = true;
            } else {
                page.shown = false;
            }
        });
    });
    $scope.search = function (name) {
        angular.forEach($scope.pages, function (page) {
            if (name == '全部') {
                return page.shown = true;
            }
            if (page.sharerName.indexOf(name) != -1) {
                page.shown = true;
            } else {
                page.shown = false;
            }
        });
    }

    $scope.sharePage = function (pageId, pageName) {
        $rootScope.$broadcast('SHARE_EV', pageId, pageName);
    }

    // 全局空白处的右键
    $('.pages-container').on('contextmenu', function (e) {
        if (e.button == 2) {
            $rootScope.$broadcast('CONTEXTMENU_EV', e.pageX, e.pageY, [
                {
                    name: '新建页面',
                    icon: 'file page icon',
                    fn: function () {
                        $rootScope.$broadcast('NEW_PAGE_EV');
                    }
                }
            ]);
            e.preventDefault();
        } else {
        }
        return false;
    });

    // 新建页面
    $scope.new_page = function () {
        $rootScope.$broadcast('NEW_PAGE_EV', function (pageId) {
            userConfig.getUserConfig().folders[$scope.folders[$scope.cur_folder_index].name].push(pageId);
        });
    }

    // 文件夹操作菜单
    var folder_menus = [
        {
            name: '删除',
            icon: 'trash icon',
            fn: function () {
                $rootScope.$broadcast('CONFIRM_EV', {
                    title: '文件夹删除提醒',
                    content: '删除此文件夹之后要删除里面的文件吗?',
                    btns: {
                        yes: {
                            text: '删除文件夹以及里面的文件',
                            fn: function () {
                                angular.forEach($scope.folders[folder_index].pages, function (page) {
                                    if (!page) {
                                        return;
                                    }
                                    tools.get({
                                        url: '/Ibm-Client/deletePage.htm',
                                        data: {
                                            pageId: page.pageId
                                        },
                                        succ: function (resp) {
                                            if (resp.success) {

                                            }
                                        }
                                    });
                                });
                                $scope.folders.splice(folder_index, 1);
                                var config = userConfig.getUserConfig();
                                config.folders = $scope.folders;
                                userConfig.updateUserConfig();
                                $timeout(function () {
                                }, 0);
                            }
                        },
                        yes2: {
                            text: '仅删除文件夹,保留文件',
                            fn: function () {

                            }
                        },
                        no: {
                            text: '取消',
                            fn: function () {

                            }
                        }
                    }
                });
            }
        }
    ];

    // 文件操作菜单
    var page_menus = [
        {
            name: '删除',
            icon: 'trash icon',
            hide: function () {
                return $scope.userName != $scope.pages[page_index].sharerName;
            },
            fn: function () {
                $rootScope.$broadcast('CONFIRM_EV', {
                    title: '页面操作提醒',
                    content: '删除之后不可恢复,确定删除吗?',
                    btns: {
                        yes: {
                            text: '确定',
                            fn: function (e) {
                                tools.get({
                                    url: '/Ibm-Client/deletePage.htm',
                                    data: {
                                        pageId: $scope.pages[page_index].pageId
                                    },
                                    succ: function (resp) {
                                        if (resp.success) {
                                            $scope.pages.splice(page_index, 1);
                                            $rootScope.$broadcast('CONFIRM_OFF_EV');
                                            page_index = -1;
                                            $scope.pages_backup = angular.copy($scope.pages);
                                        }
                                    }
                                });
                            }
                        },
                        no: {
                            text: '取消',
                            fn: function () {

                            }
                        }
                    }
                });
            }
        },
        {
            name: '分享',
            icon: 'share alternate icon',
            fn: function () {
                $rootScope.$broadcast('SHARE_EV', $scope.pages[page_index].pageId, $scope.pages[page_index].pageName);
            }
        }
    ];

    // 上下文操作
    $('.pages-container').delegate('.column.page', 'contextmenu', function (e) {
        if ($(e.target).closest('.column.page').hasClass('folder')) {
            $rootScope.$broadcast('CONTEXTMENU_EV', e.pageX, e.pageY, folder_menus);
            folder_index = $(e.target).closest('.column').index() - 1;
            return false;
        } else {
            $rootScope.$broadcast('CONTEXTMENU_EV', e.pageX, e.pageY, page_menus);
            page_index = $(e.target).closest('.column').index() - 1 - $scope.folders.length;
            return false;
        }
    });

    // 获取所有的页面
    $rootScope.getAllPages = function () {
        return $scope.pages;
    }

    // 根据pageID获取page对象
    $scope.getPageById = function (pageId, pages) {
        for (var i = 0; i < pages.length; i++) {
            if (pageId === pages[i].pageId) {
                var deledPage = pages.splice(i, 1)[0];
                return deledPage;
            }
        }

        return null;
    }

    // 是否显示文件夹,当在文件夹内时不显示所有的文件夹
    $scope.folders_shown = true;

    // 编辑文件夹
    $scope.editFolder = function (e, i) {
        $rootScope.$broadcast('EDIT_FOLDER_EV', $scope.folders[i]);
        e.stopPropagation();
    }

    // 新建文件夹
    $scope.newFolder = function () {
        $rootScope.$broadcast('NEW_FOLDER_EV');
    }

    // 删除文件夹
    // $scope.delFolder = function () {
    //
    // }

    // 返回
    $scope.back = function () {
        $timeout(function () {
            $scope.pages = angular.copy($scope.pages_backup);
            $scope.folders_shown = true;

            $scope.folders.length = 0;
            // 整理文件夹数据
            var folders = userConfig.getUserConfig().folders;
            for (var i in folders) {
                var folder = {
                    name: i,
                    pages: []
                };

                for (var j = 0; j < folders[i].length; j++) {
                    folder.pages.push($scope.getPageById(folders[i][j], $scope.pages));
                }

                $scope.folders.push(folder);
            }
        }, 0);
    };

    // 进文件夹
    $scope.intoFolder = function (i) {
        $scope.pages = $scope.folders[i].pages;
        $scope.folders_shown = false;
        $scope.cur_folder_index = i;
    };

    // 刷新
    $scope.refresh = function () {
        tools.get({
            url: '/Ibm-Client/getPages.htm',
            succ: function (resp) {
                if (resp.success) {
                    $scope.pages = resp.value;
                    $scope.folders.length = 0;

                    angular.forEach($scope.pages, function (page) {
                        page.shown = true;
                    });

                    $scope.pages_backup = angular.copy($scope.pages);

                    // 整理文件夹数据
                    var folders = userConfig.getUserConfig().folders;
                    for (var i in folders) {
                        var folder = {
                            name: i,
                            pages: []
                        };

                        for (var j = 0; j < folders[i].length; j++) {
                            folder.pages.push($scope.getPageById(folders[i][j], $scope.pages));
                        }

                        $scope.folders.push(folder);
                    }
                }
            }
        });
    }

    // 编辑文件
    $scope.toEditor = function (e, pageId, tbId, tableType, sharerName) {
        $location.url('/editor?id=' + pageId + '&tbId=' + tbId + '&tableType=' + tableType + '&type=editor&sharerName=' + sharerName);
        e.stopPropagation();
    }

    // 查看报告
    $scope.toProd = function (e, pageId, tbId, tableType, sharerName) {
        if (tools.mobileEnv()) {
            $location.url('/mobileEditor?id=' + pageId + '&tbId=' + tbId + '&tableType=' + tableType + '&type=prod');
        } else {
            $location.url('/editor?id=' + pageId + '&tbId=' + tbId + '&tableType=' + tableType + '&type=prod&sharerName=' + sharerName);
        }
    }

    // 是否是超级大屏
    $scope.isSuperScreen = function () {
        return window.screen.width >= 1800;
    }

    // 新文件夹添加事件
    var UPDATE_FOLDER_INFO_OFF = $rootScope.$on('UPDATE_FOLDER_INFO', function () {
        console.log(userConfig.getUserConfig());
        $scope.refresh();
    });

    $scope.$on('$destroy', function () {
        for (var i in events) {
            if (i.indexOf('_OFF') != -1) {
                events[i]();
            }
        }
    });

    $scope.refresh();
}]);
services.factory('pagesServ', ['tools', "$rootScope", function (tools, $rootScope) {
    return {
        getPageClassifies: function () {
            return [];
        }
    }
}]);